package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.ContractDao;
import cn.ym.mall.entity.Contract;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class ContractDaoImpl implements ContractDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 获得所有合同(带分页信息)
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List<Contract> getAll(Integer pageNo, Integer pageSize) throws SQLException {
        String sql="select coid, sid, cocreateTime, cocontent, coyear, codividend from ym_contract order by  cocreateTime desc limit ?,?;";
        List<Contract> contractList=null;
        try{
            contractList= queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Contract>(Contract.class),(pageNo-1)*pageSize,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        return contractList;
    }

    /**
     * 添加新合同
     *
     * @param contract
     * @return
     * @throws SQLException
     */
    @Override
    public int add(Contract contract) throws SQLException {
        String sql="insert into ym_contract(sid, cocontent, coyear, codividend) value(?,?,?,?);";
        Object arr[] ={contract.getSid(),contract.getCocontent(),contract.getCoyear(),contract.getCodividend()};
        int num=0;
        try {
            num=queryRunner.update(DruidUtils.getConnection(),sql,arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 根据合同编号查询合同
     *
     * @param coid 合同编号(id)
     * @return
     * @throws SQLException
     */
    @Override
    public Contract getContractByCoid(Integer coid) throws SQLException {
        String sql="select coid, sid,cocontent, coyear, codividend from ym_contract where coid=?;";
        Object arr[] ={coid};
        Contract contract = null;
        try {
            contract=queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<Contract>(Contract.class),arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return contract;
    }

    /**
     * 根据商店编号查询合同
     *
     * @param sid 商店编号(id)
     * @return
     * @throws SQLException
     */
    @Override
    public Contract getContractBySid(Integer sid) throws SQLException {
        String sql="select coid, sid,cocontent, coyear, codividend from ym_contract where sid=?;";
        Object arr[] ={sid};
        Contract contract = null;
        try {
            contract=queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<Contract>(Contract.class),arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return contract;
    }

    /**
     * 合同总数
     *
     * @return
     * @throws SQLException
     */
    @Override
    public long getCount() throws SQLException {
        String sql = "select count(0) as count from ym_contract;";
        long count= 0;
        try {
            count = queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<>());
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }
}
